//
//  AllHeroViewController.m
//  LOL_PalmarHelper
//
//  Created by dllo on 15/6/19.
//  Copyright (c) 2015年 hantao. All rights reserved.
//

#import "AllHeroViewController.h"

@interface AllHeroViewController ()

@end

@implementation AllHeroViewController


-(void)dealloc
{
    [_mySearch release];
    [_heroArray release];
    [_collect release];
    [super dealloc];


}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];

    
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumInteritemSpacing = 5;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    flowLayout.itemSize = CGSizeMake((self.view.bounds.size.width - 20 - 15) / 4, ((self.view.bounds.size.width - 20 - 15) / 4) + 25);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;

    

    self.collect = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - self.navigationController.navigationBar.bounds.size.height - self.navigationController.tabBarController.tabBar.bounds.size.height - 20) collectionViewLayout:flowLayout];
    self.collect.backgroundColor = [UIColor whiteColor];
    self.collect.delegate = self;
    self.collect.dataSource = self;
    
    [self.collect registerClass:[AllHeroCollectionViewCell class] forCellWithReuseIdentifier:@"Reuse"];
    [self.view addSubview:self.collect];

    self.heroArray = [NSMutableArray array];
    [self requestData];
    
    self.collect.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(freshAction)];


    
    
    
    
}




-(void)freshAction
{

    [self requestData];

    
}




-(void)requestData
{

    
   
    
    MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.view];
    HUD.labelText = @"HHZ小组正卖力加载中";
    [self.view addSubview:HUD];
    [HUD show:YES];
    [HUD release];
    
    
    [[HeroBaseHandle ShareHeroHeroDate] openDB];
   
    if ([[HeroBaseHandle ShareHeroHeroDate] selectAllHero] != nil) {
        
            self.heroArray = [[HeroBaseHandle ShareHeroHeroDate] selectAllHero];

    }
    
    
    
[AFN AFNGetData:@"http://lolbox.duowan.com/phone/apiHeroes.php?type=all" andBlock:^(id respondes) {
    

        if (![respondes isKindOfClass:[@"error" class]]) {
      
        
            
            [[HeroBaseHandle ShareHeroHeroDate] openDB];
            NSMutableDictionary *dic = respondes;
            NSArray *array = [dic objectForKey:@"all"];
            [[HeroBaseHandle ShareHeroHeroDate] dropAllHerotable];
            [self.heroArray removeAllObjects];
            [[HeroBaseHandle ShareHeroHeroDate] createAllHeroTable];
            for (NSMutableDictionary *Dic in array) {
                
                
                HeroModel *hero = [[HeroModel alloc] init];
                
                [hero setValuesForKeysWithDictionary:Dic];
                
                [[HeroBaseHandle ShareHeroHeroDate] insertAllHero:hero];
                [self.heroArray addObject:hero];
                [hero release];
            }
            [self.collect reloadData];
            [self.collect.header endRefreshing];

            [HUD hide:YES];
        }else if ([respondes isEqualToString:@"error"]) {
            

            
            UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络不给力" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [self.view addSubview:al];
            [al show];
            [al release];
            
            
            [HUD hide:YES];
            [self.collect.header endRefreshing];

        }
    
    
}];
    

    [self.collect.header endRefreshing];
   
}



-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    
    HeroDetailViewController *heroDetail = [[HeroDetailViewController alloc] init];
    HeroModel *hero = [self.heroArray objectAtIndex:indexPath.row];
    heroDetail.enName = hero.enName;
    [self.navigationController pushViewController:heroDetail animated:YES];
    


}





-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{

    return self.heroArray.count;
}



-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    AllHeroCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Reuse" forIndexPath:indexPath];
    cell.layer.transform = CATransform3DMakeScale(0.3, 0.3, 0.1);
    [UIView animateWithDuration:0.5 animations:^{
        
        cell.layer.transform = CATransform3DMakeScale(1, 1, 0.1);
       
   
    }];

   
    cell.backgroundColor = [UIColor whiteColor];

    cell.hero = [self.heroArray objectAtIndex:indexPath.row];
    
    return cell;
}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
